package com.yh.csx.etl.worker;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.yh.csx.etl.config.EtlDataMappingManager;

/**
 * @author Robin.Wang
 * @date 2020-03-17
 *
 */
@Component
public class EtlWorkerManager implements LifeCycle {

    private static final Logger log = LoggerFactory.getLogger(EtlWorkerManager.class);
    @Autowired
    ThreadPoolTaskExecutor taskExecutor;

    @Override
    public void init() {
        EtlDataMappingManager.loadConfigs("");
    }

    @Override
    public void start() {
        processData();
    }

    @Override
    public void stop() {
    }

    public void processData() {
        log.info("开始处理数据");
        List<Future> futures = new ArrayList<Future>();
        TreeSet<String> set = new TreeSet<String>();
        set.addAll(EtlDataMappingManager.MAPPINGS.keySet());
        set.descendingSet().forEach((key) -> {
            EtlWorker worker = new EtlWorker(EtlDataMappingManager.MAPPINGS.get(key));
            String[] sourceTables = EtlDataMappingManager.MAPPINGS.get(key).getDbMapping().getTable().split(",");
            for (int i = 0; i < sourceTables.length; i++) {
                String sourceTable = sourceTables[i].trim();
                Future future = taskExecutor.submit(() -> {
                    try {
                        worker.importData(sourceTable, null);
                    } catch (Exception e) {
                        log.error("导入数据异常:{}", e);
                    }
                });
                futures.add(future);
            }
            log.info("开始处理数据表{}", key);

        });
        for (Future f : futures) {
            try {
                f.get();
            } catch (InterruptedException | ExecutionException e) {

            }
        }
        log.info("处理完成");
    }

}
